# -*- coding: utf-8 -*-

# File Name： bbox_util
# Description :
# Author : lirui
# create_date： 2022/6/6
# Change Activity:
import cv2
import numpy as np

from ml.cv.algorithms.sort_points.sort_points import resort_points
from ml.cv.util.distance import get_distance


def enlarge_bbox(enlarge_pixel, bbox, shape):
    """

    Args:
        enlarge_pixel:
        bbox:
        shape:

    Returns:

    """
    p1, p2, p3, p4 = bbox[0], bbox[1], bbox[2], bbox[3]
    p1 = [p1[0] - enlarge_pixel, p1[1] - enlarge_pixel]
    p2 = [p2[0] + enlarge_pixel, p2[1] - enlarge_pixel]
    p3 = [p3[0] + enlarge_pixel, p3[1] + enlarge_pixel]
    p4 = [p4[0] - enlarge_pixel, p4[1] + enlarge_pixel]
    bbox = [p1, p2, p3, p4]
    img_height, img_width = shape[0], shape[1]
    for point in bbox:
        width, height = point
        if width < 0:
            point[0] = 0
        if height < 0:
            point[1] = 0
        if height > img_height:
            point[1] = img_height
        if width > img_width:
            point[0] = img_width
    return bbox


def enlarge_vertical_bbox(enlarge_pixel, bbox, shape):
    """

    Args:
        enlarge_pixel:
        bbox:
        shape:

    Returns:

    """
    p1, p2, p3, p4 = bbox[0], bbox[1], bbox[2], bbox[3]
    p1 = [p1[0], p1[1] - enlarge_pixel]
    p2 = [p2[0], p2[1] - enlarge_pixel]
    p3 = [p3[0], p3[1] + enlarge_pixel]
    p4 = [p4[0], p4[1] + enlarge_pixel]
    bbox = [p1, p2, p3, p4]
    img_height, img_width, _ = shape
    for point in bbox:
        width, height = point
        if width < 0:
            point[0] = 0
        if height < 0:
            point[1] = 0
        if height > img_height:
            point[1] = img_height
        if width > img_width:
            point[0] = img_width
    return bbox


def _get_area_shape(bbox):
    """
    get bbox width and height.
    Args:
        bbox: (p0,p1,p2,p3)

    Returns:

    """
    len_1, len_2 = get_distance(bbox[0], bbox[1]), get_distance(bbox[1], bbox[2])
    return int(len_1), int(len_2)


def _rotate_image(bbox, dst):
    """
    pass
    """
    center_point_1 = bbox[0][0] + bbox[1][0], bbox[0][1] + bbox[1][1]
    center_point_2 = bbox[2][0] + bbox[3][0], bbox[2][1] + bbox[3][1]
    if center_point_1[0] < center_point_2[0]:
        left_height = center_point_1[1]
        right_height = center_point_2[1]
    else:
        left_height = center_point_2[1]
        right_height = center_point_1[1]
    if right_height < left_height:
        dst = np.rot90(dst)
        dst = np.rot90(dst)
        dst = np.rot90(dst)
    else:
        dst = np.rot90(dst)
    return dst


def get_textarea_from_bbox(original_image, bbox):
    """
    split textarea from bbox.

    Args:
        original_image:
        bbox:

    Returns:

    """
    bbox = resort_points(bbox)
    area_width, area_height = _get_area_shape(bbox)
    pts1 = np.float32([bbox[0], bbox[1], bbox[3], bbox[2]])
    pts2 = np.float32([[0, 0], [0, area_width], [area_height, 0], [area_height, area_width]])
    transform = cv2.getPerspectiveTransform(pts1, pts2)
    dst = cv2.warpPerspective(original_image, transform, (area_height, area_width))
    dst = np.rot90(dst)
    dst = np.flip(dst, axis=0)
    if area_height > area_width:
        dst = _rotate_image(bbox, dst)
    if area_width < 6 or area_height < 6:
        return None
    return dst
